package apps.generate.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import apps.generate.domain.DBFieldBean;
import apps.generate.service.DBFieldService;
import core.tool.common.StringHelper;
import core.tool.common.SystemInfo;
import core.tool.template.freemarker.FreemarkerUtil;
import core.web.easyui.GridColumn;

@Controller
@RequestMapping("/db")  
public class DBController {
	
    @Autowired
	private DBFieldService dbFieldService;
    
    
    /**
     * 生成的表格字段名称是根据表字段名称的大写形式，生成的数据的key是javabean中的字段名称，两个方式不一样
     * @param tableName
     * @return
     */
	@RequestMapping("/columns")
    public @ResponseBody Object getColumns(@RequestParam("t")String tableName) {
		tableName=tableName.toUpperCase();
		List<GridColumn> list=dbFieldService.getGridColumns(tableName);
		return list;
    }

	
	/**
	 * spring mvc返回字符串会出现乱码问题，返回json（通过返回对象的形式）就不会有这个问题；使用原生HttpServletResponse的方式解决
	 */
	@RequestMapping("/javaBean")	
	public void getJavaBean1(@RequestParam(value="t", required=false, defaultValue="dsfa_users")String tableName,HttpServletResponse response) {
		tableName=tableName.toUpperCase();
		List<DBFieldBean> list=dbFieldService.getDBFieldBeans(tableName);
		for(DBFieldBean dbFieldBean:list) {
//			dbFieldBean.setFieldName(dbFieldBean.getFieldName().toLowerCase());
			dbFieldBean.setJavaFieldName(dbFieldBean.getFieldName().toLowerCase().replace("_", ""));
		}		
		String directoryPath=SystemInfo.ROOT_PATH+"/WEB-INF/template";
		String ftlName="domain.ftl";
		FreemarkerUtil freemarkerUtil=new FreemarkerUtil(directoryPath,ftlName,null);
		freemarkerUtil.puttoroot("tableName", tableName);
		freemarkerUtil.puttoroot("dBFieldBeanList", list);	
		String result=freemarkerUtil.getTemplateStr();	
		StringHelper.writeStrToResponse(response, result);
	}	

	@RequestMapping("/jdbcquery")	
	public void getJdbcQuery(@RequestParam(value="t", required=false, defaultValue="dsfa_users")String tableName,@RequestParam(value="b")String beanName,HttpServletResponse response) {
		StringBuffer result=new StringBuffer();
		tableName=tableName.toUpperCase();
		List<DBFieldBean> list=dbFieldService.getDBFieldBeans(tableName);
		for(DBFieldBean dbFieldBean:list) {
			String lowwerName=dbFieldBean.getFieldName().toLowerCase().replace("_", "");
			String rSet="rSet.getString(\""+dbFieldBean.getFieldName()+"\")";
			result.append(beanName+".set"+StringHelper.getFirstUpper(lowwerName)+"("+rSet+");");
			result.append("</br>");
		}		
		StringHelper.writeStrToResponse(response, result.toString());
	}	
	
	@RequestMapping("/jdbcinsert")	
	public void getJdbcInsert(@RequestParam(value="t")String tableName,HttpServletResponse response) {
		StringBuffer result=new StringBuffer();
		StringBuffer second=new StringBuffer();
		result.append("INSERT INTO ");
		result.append(tableName+"(");
		tableName=tableName.toUpperCase();
		List<DBFieldBean> list=dbFieldService.getDBFieldBeans(tableName);
		for(DBFieldBean dbFieldBean:list) {
			String lowwerName=dbFieldBean.getFieldName().toLowerCase().replace("_", "");
			result.append(dbFieldBean.getFieldName()+",");
		}	
		result=new StringBuffer(result.substring(0,result.toString().length()-1));
		result.append(")VALUES(");
		for(DBFieldBean dbFieldBean:list) {
			result.append("?,");
			String lowwerName=dbFieldBean.getFieldName().toLowerCase().replace("_", "");
			second.append("list.add(new Parameter(\""+dbFieldBean.getFieldName()+"\",DBHelper.DataType.STRING,getStrOfParmeter(flowInstanceEntity.get"+StringHelper.getFirstUpper(lowwerName)+"())));");
			second.append("</br>");
		}	
		result=new StringBuffer(result.substring(0,result.toString().length()-1));
		result.append(")");
		result.append("</br>");
		StringHelper.writeStrToResponse(response, result.toString());
		StringHelper.writeStrToResponse(response, second.toString());
	}	
	
	@RequestMapping("/toString")	
	public void getToString(@RequestParam(value="t")String tableName,HttpServletResponse response) {
		tableName=tableName.toUpperCase();
		List<DBFieldBean> list=dbFieldService.getDBFieldBeans(tableName);
		for(DBFieldBean dbFieldBean:list) {
			dbFieldBean.setFieldName(dbFieldBean.getFieldName().toLowerCase().replace("_", ""));
		}	
		String directoryPath=SystemInfo.ROOT_PATH+"/WEB-INF/template";
		String ftlName="toString.ftl";
		FreemarkerUtil freemarkerUtil=new FreemarkerUtil(directoryPath,ftlName,null);
		freemarkerUtil.puttoroot("dBFieldBeanList", list);	
		String result=freemarkerUtil.getTemplateStr();	
		StringHelper.writeStrToResponse(response, result.toString());
	}		
	
	
	public DBFieldService getDbFieldService() {
		return dbFieldService;
	}

	public void setDbFieldService(DBFieldService dbFieldService) {
		this.dbFieldService = dbFieldService;
	}
	
    
    
	

}
